package com.example.administrator.xxnapp.fragment;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.example.administrator.xxnapp.fragment.event.EventFragment;
import com.example.administrator.xxnapp.fragment.mine.MineFragment;
import com.example.administrator.xxnapp.fragment.picture.PictureFragment;
import com.example.administrator.xxnapp.fragment.tucao.TuCaoFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页中Fragment的管理类
 * Created by Administrator on 2018/4/22.
 */

public class MainFragmentManager {
    private List<Fragment> mFragments;
    private TuCaoFragment mTuCaoFragment;
    private EventFragment mEventFragment;
    private PictureFragment mPictureFragment;
    private MineFragment mMineFragment;
    private Context context;
    private FragmentManager mFM;
    private Fragment mCurentFragment;
    private int layoutResId;

    public MainFragmentManager(Context context){
        this.context = context;
        mFragments = new ArrayList<>();
        mMineFragment = MineFragment.getInstance();
        mEventFragment = EventFragment.getInstance();
        mPictureFragment = PictureFragment.getInstance();
        mTuCaoFragment = TuCaoFragment.getInstance();

        mFragments.add(mTuCaoFragment);
        mFragments.add(mEventFragment);
        mFragments.add(mPictureFragment);
        mFragments.add(mMineFragment);

        mFM = ((AppCompatActivity)context).getSupportFragmentManager();
    }
    public MainFragmentManager(Context context, int layoutResId){
        this(context);
        this.layoutResId = layoutResId;
    }

    public void chooseFragment(int position,int layoutResId){
        FragmentTransaction fragmentTransaction = mFM.beginTransaction();
        for (int i = 0, j = mFragments.size(); i < j; i++) {
            if (i == position) {
                continue;
            }
            mCurentFragment = mFragments.get(i);
            if (mCurentFragment.isAdded()) {
                fragmentTransaction.hide(mCurentFragment);
            }
        }
        mCurentFragment = mFragments.get(position);

        if (mCurentFragment.isAdded()) {
            fragmentTransaction.show(mCurentFragment);
        } else {
            /*fragmentTransaction.replace(layoutResId,mCurentFragment);
            if (isSavedInstanceState){
                isSavedInstanceState = false;
            }else {
            }*/
            fragmentTransaction.add(layoutResId, mCurentFragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
    }
    public void chooseFragment(int position){
        if (position>-1 && position<mFragments.size()){
            FragmentTransaction fragmentTransaction = mFM.beginTransaction();
            for (int i = 0, j = mFragments.size(); i < j; i++) {
                if (i == position) {
                    continue;
                }
                mCurentFragment = mFragments.get(i);
                if (mCurentFragment.isAdded()) {
                    fragmentTransaction.hide(mCurentFragment);
                }
            }
            mCurentFragment = mFragments.get(position);

            if (mCurentFragment.isAdded()) {
                fragmentTransaction.show(mCurentFragment);
            } else {
            /*fragmentTransaction.replace(layoutResId,mCurentFragment);
            if (isSavedInstanceState){
                isSavedInstanceState = false;
            }else {
            }*/
                fragmentTransaction.add(layoutResId, mCurentFragment);
            }
            fragmentTransaction.commitAllowingStateLoss();
        }
    }
}
